package com.wx.cloud.dailyreport.service.impl;


import com.wx.cloud.dailyreport.Model.Dailyreport;
import com.wx.cloud.dailyreport.common.CommonResult;
import com.wx.cloud.dailyreport.feign.IUserService;
import com.wx.cloud.dailyreport.service.DailyreportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DailyreportServiceImpl implements DailyreportService {
    @Autowired
    private IUserService iUserService;

    @Autowired
    private RestTemplate restTemplate;

    List<Dailyreport> dailyreports = new ArrayList();

    @PostConstruct
    private void initDailyreport(){
        Dailyreport english = Dailyreport.builder().id(1).english("english").chinese("英语").userId(1).build();
        Dailyreport chinese = Dailyreport.builder().id(2).english("chinese").chinese("汉语").userId(2).build();
        Dailyreport dailyreport = Dailyreport.builder().id(3).english("dailyreport").chinese("日报").userId(3).build();
        dailyreports.add(english);
        dailyreports.add(chinese);
        dailyreports.add(dailyreport);
    }

//    public CommonResult getDailyreportsByUserName(String name) {
//        HashMap<String,String> map=new HashMap();
//        map.put("name",name);
//        CommonResult forObject = restTemplate.getForObject("http://user/user/getDailyreportsByUserName?name={name}", CommonResult.class, map);
//        Object data = forObject.getData();
//        Integer id = (Integer) data;
//        List<Dailyreport> filter = dailyreports.stream().filter(d -> d.getUserId() == id).collect(Collectors.toList());
//        return CommonResult.success(filter);
//    }

    public CommonResult getDailyreportsByUserName(String name) {
        CommonResult dailyreportsByUserName = iUserService.getDailyreportsByUserName(name);
        if (dailyreportsByUserName.getCode()==500){
            return dailyreportsByUserName;
        }
        Object data = dailyreportsByUserName.getData();
        Integer id = (Integer)data;
        List<Dailyreport> filter = dailyreports.stream().filter(d -> d.getUserId() == id).collect(Collectors.toList());
        return CommonResult.success(filter);
    }
}
